package com.yc.rpc.core;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 冉野 E-mail:yangtianrui135@163.com
 * @version 创建时间：2021/7/13 4:00 PM
 */
@Component
public class ReflectionRpc implements BeanFactoryAware {

    BeanFactory beanFactory = null;
    
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
    
    
    public Object getObject(Metadata metadata) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        Class<?> classType=Class.forName(metadata.getClazz());
        Object metadataObject = beanFactory.getBean(classType);

        Method method = metadataObject.getClass().getMethod(metadata.getMethod(), metadata.getParameterTypes());
        return method.invoke(metadataObject,metadata.parameters);

    }
}
